<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changeFn"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          node-key="id"
          @node-click="handlerNodeClick"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="tableData">
          <el-table-column
            align="center"
            label="头像"
            prop="staffPhoto"
          >
            <template v-slot="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="姓名"
            prop="username"
          />
          <el-table-column
            label="手机号"
            sortable
            prop="mobile"
            width="110"
          />
          <el-table-column
            label="工号"
            sortable
            prop="workNumber"
            width="110"
          />
          <el-table-column
            label="聘用形式"
            prop="formOfEmployment"
          >
            <template v-slot="{row}">
              <span>
                {{ row.formOfEmployment === 1?
                  '正式' : row.formOfEmployment === 2?
                    '非正式': '无' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="部门"
            prop="departmentName"
          />
          <el-table-column
            width="110"
            label="入职时间"
            sortable
            prop="timeOfEntry"
          />
          <el-table-column
            label="操作"
            width="200px"
          >
            <template v-slot="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text">角色</el-button>
              <el-popconfirm title="确认删除该行数据吗？" @onConfirm="delEmployee(row.id)">
                <el-button
                  slot="reference"
                  style="margin-left:10px"
                  size="mini"
                  type="text"
                >删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="end" style="height:60px" align="middle">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :current-page.sync="queryParams.page"
            :page-size="queryParams.pagesize"
            @current-change="getEmployeeList"
          />
        </el-row>
      </div>
      <ImportExcel
        :show-excel-dialog.sync="showExcelDialog"
        @upload="getEmployeeList"
      />
    </div>
  </div>
</template>

<script>
import { getDepartmentList } from '@/api/department'
import { transeListToTree } from '@/utils/index'
import { getEmployeeList, exportEmployee, delEmployee } from '@/api/employee'
import FileSaver from 'file-saver'
import ImportExcel from '@/views/employee/components/ImportExcel.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      queryParams: {
        departmentId: null,
        page: 1,
        pagesize: 8,
        keyword: ''
      },
      tableData: [],
      total: 0,
      showExcelDialog: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    handlerNodeClick(node) {
      this.queryParams.departmentId = node.id
      this.queryParams.page = 1
      this.getEmployeeList()
    },
    changeFn() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployeeList()
      }, 500)
    },
    async getDepartmentList() {
      this.depts = transeListToTree(await getDepartmentList(), 0)
      this.queryParams.departmentId = this.depts[0].id
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.queryParams.departmentId)
      })
      this.getEmployeeList()
    },
    async getEmployeeList() {
      const { rows, total } = await getEmployeeList({ ...this.queryParams })
      this.tableData = rows
      this.total = total
    },
    async exportEmployee() {
      const res = await exportEmployee()
      FileSaver.saveAs(res, '员工信息表.xlsx')
    },
    async delEmployee(id) {
      await delEmployee(id)
      console.log(id)
      if (this.tableData.length === 1 && this.queryParams.page > 1) this.queryParams.page--
      this.getEmployeeList()
      this.$message.success('删除员工成功')
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
